package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class books_List extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public books_List() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    //根据条件返回小说信息
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        PrintWriter out = response.getWriter();
        JSONArray jsonarray = new JSONArray();
        JSONObject jsonobj = new JSONObject();


        conn cn = new conn();

        String f = request.getParameter("f");

        ResultSet rs = null;

        if (f.equals("1")) {
            rs = cn.query("select * from  books  order by btotal desc limit 0,10");
        } else if (f.equals("2")) {
            String caid = request.getParameter("caid");
            String keys = request.getParameter("keys");

            String where = " where 1=1 ";
            if (!caid.equals("0")) {
                where += " and a.caid=" + caid;
            }

            if (keys != null && !keys.equals("")) {
                where += " and (bname like '%" + keys + "%' or author like '%" + keys + "%' )";
            }

            if (caid.equals("0")) {
                rs = cn.query("select * from  books a " + where + " order by bid desc ");
            } else {
                rs = cn.query("select * from  books a " + where + "  order by bid desc ");
            }

        } else if (f.equals("3")) {
            String bid = request.getParameter("bid");
            cn.update("update books set btotal=btotal+1 where bid=" + bid);
            rs = cn.query("select * from  books   where bid=" + bid);
        } else if (f.equals("4")) {
            String mid = request.getParameter("mid");
            rs = cn.query("select * from  bmore   where mid=" + mid);
        } else if (f.equals("5")) {
            String mid = request.getParameter("mid");
            String bid = request.getParameter("bid");
            rs = cn.query("select * from  bmore   where bid=" + bid + " and mid<" + mid + " order by mid desc limit 0,1");
        } else if (f.equals("6")) {
            String mid = request.getParameter("mid");
            String bid = request.getParameter("bid");
            rs = cn.query("select * from  bmore   where bid=" + bid + " and mid>" + mid + " order by mid  limit 0,1");
        } else if (f.equals("7")) {
            String mid = request.getParameter("mid");
            String mid2 = request.getParameter("mid2");
            String bid = request.getParameter("bid");
            if (mid.equals("0")) {

                if (cn.exist("select * from mreads where mid2=" + mid2 + " and bid=" + bid)) {

                    rs = cn.query("select * from  bmore " +
                            "  where  mid in(select max(mid) from mreads where mid2=" + mid2 + " and bid=" + bid + " group by bid) ");

                } else {
                    rs = cn.query("select * from  bmore   where   bid=" + bid + " order by  mid  limit 0,1");
                }

            } else {
                rs = cn.query("select * from  bmore   where   mid=" + mid + " ");
            }


        } else if (f.equals("8")) {
            rs = cn.query("select * from  books   order by btotal desc  limit 0,5");
        } else if (f.equals("9")) {
            rs = cn.query("select * from  books   order by bid desc  limit 0,5");
        }
        else if (f.equals("10")) {
            String caid = request.getParameter("caid");


            String where = " where 1=1 ";
            if (!caid.equals("0")) {
                where += " and a.caid=" + caid;
            }



            if (caid.equals("0")) {
                rs = cn.query("select * from  books a " + where + " order by btotal desc limit 0,10");
            } else {
                rs = cn.query("select * from  books a " + where + "  order by btotal desc limit 0,10");
            }

        }

        while (true) {
            try {
                if (!rs.next()) break;


                if (f.equals("4") || f.equals("5") || f.equals("6") || f.equals("7")) {
                    jsonobj.put("mid", rs.getString("mid"));
                    jsonobj.put("bid", rs.getString("bid"));
                    jsonobj.put("mtitle", rs.getString("mtitle"));
                    jsonobj.put("memo", rs.getString("memo"));

                    String bid = request.getParameter("bid");

                    ResultSet rs2 = cn.query("select * from  bmore where bid=" + bid);
                    JSONArray jsonArray2 = new JSONArray();
                    JSONObject jsonobj2 = new JSONObject();
                    while (rs2.next()) {
                        jsonobj2.put("mid", rs2.getString("mid"));
                        jsonobj2.put("bid", rs2.getString("bid"));
                        jsonobj2.put("mtitle", rs2.getString("mtitle"));
                        jsonobj2.put("memo", rs2.getString("memo"));
                        jsonArray2.add(jsonobj2);
                    }

                    jsonobj.element("bmore", jsonArray2);
                    jsonarray.add(jsonobj);
                } else {

                    jsonobj.put("bid", rs.getString("bid"));
                    jsonobj.put("caid", rs.getString("caid"));
                    jsonobj.put("bname", rs.getString("bname"));
                    jsonobj.put("bpic", rs.getString("bpic"));
                    jsonobj.put("author", rs.getString("author"));
                    jsonobj.put("words", rs.getString("words"));
                    jsonobj.put("price", rs.getString("price"));
                    jsonobj.put("bmemo", rs.getString("bmemo").replaceAll(request.getContextPath(),"http://localhost:8080/"+request.getContextPath()).replaceAll("<img","<img style=='width:100%' "));
                    jsonobj.put("btotal", rs.getString("btotal"));
                    jsonobj.put("btime", rs.getString("btime"));

					if (f.equals("3")) {
						String bid = request.getParameter("bid");
						ResultSet rs2 = cn.query("select * from  bmore where bid=" + bid);
						JSONArray jsonArray2 = new JSONArray();
						JSONObject jsonobj2 = new JSONObject();
						while (rs2.next()) {
							jsonobj2.put("mid", rs2.getString("mid"));
							jsonobj2.put("bid", rs2.getString("bid"));
							jsonobj2.put("mtitle", rs2.getString("mtitle"));
							jsonobj2.put("memo", rs2.getString("memo"));
							jsonArray2.add(jsonobj2);
						}

						jsonobj.element("bmore", jsonArray2);


						ResultSet rs3 = cn.query("select * from  comment a left join member m on a.mid = m.mid" +
								" where bid=" + bid);
						JSONArray jsonArray3 = new JSONArray();
						JSONObject jsonobj3 = new JSONObject();
						while (rs3.next()) {
							jsonobj3.put("nic", rs3.getString("nic"));
							jsonobj3.put("photo", rs3.getString("photo"));
							jsonobj3.put("cmemo", rs3.getString("cmemo"));
							jsonobj3.put("ctime", rs3.getString("ctime"));
							jsonArray3.add(jsonobj3);
						}

						jsonobj.element("com", jsonArray3);

						String mid = request.getParameter("mid");
                        if (cn.exist("select * from bookshelf where mid="+mid+" and bid="+rs.getString("bid"))) {
                            jsonobj.put("flag", 1);
                        } else {
                            jsonobj.put("flag", 0);
                        }
					}

                    jsonarray.add(jsonobj);
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        out.println(jsonarray);
        out.flush();
        out.close();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

